//
//  HDRootSearchVC.m
//  HDWuDaDaoServiceCenter
//
//  Created by HDNiuKuiming on 2017/4/10.
//  Copyright © 2017年 HDNiuKuiming. All rights reserved.
//

#import "HDRootSearchVC.h"
#import "HDBottomBarView.h"
#import "HDRootSearchCell.h"
#import "HDBtnView.h"
#import "HDWebVC.h"
#import "HDAroundVC.h"
#import "HDSearchObj.h"

@interface HDRootSearchVC ()<HDBottomBarViewDelegate,UITableViewDelegate,UITableViewDataSource,HDBtnViewDelegate>
@property (weak, nonatomic) IBOutlet UIView *bottomView;
@property (weak, nonatomic) IBOutlet UITableView *myTableView;
@property (nonatomic, strong) HDSearchObj *searchObj;
@property (weak, nonatomic) IBOutlet UIView *topBar;

@end

@implementation HDRootSearchVC

#pragma mark
#pragma mark -------- Init Methods --------

- (void)viewDidLoad {
    [super viewDidLoad];
    [self barInit];
    [self tableviewInit];
    
    [self contentRequestAction];
}

- (void)barInit
{
    HDBottomBarView *bar = [[[NSBundle mainBundle] loadNibNamed:@"HDBottomBarView" owner:self options:nil] lastObject];
    [_bottomView addSubview:bar];
    [bar mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_bottomView).with.offset(0);
        make.right.equalTo(_bottomView).with.offset(0);
        make.left.equalTo(_bottomView).with.offset(0);
        make.bottom.equalTo(_bottomView).with.offset(0);
    }];
    [bar hightedBtn:3];
    bar.delegate = self;
}

- (void)barSelectAction:(NSString *)VCStr
{
    NSArray *vcArray = self.navigationController.viewControllers;
    BOOL isVCExit = NO;
    for (UIViewController *vc in vcArray) {
        if ([vc isKindOfClass:NSClassFromString(VCStr)]) {
            isVCExit = YES;
            [self.navigationController popToViewController:vc animated:NO];
            break;
        }
    }
    
    if (isVCExit == NO) {
        UIStoryboard *board = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
        UIViewController *pushVC = [board instantiateViewControllerWithIdentifier:VCStr];
        [self.navigationController pushViewController:pushVC animated:NO];
    }
}

- (void)contentRequestAction
{
    HDRequestObj *loginObj = [[HDRequestObj alloc] init];
    NSString *urlPath = [NSString stringWithFormat:@"%@g=mapi&m=Contentinfo&a=discovery", HTTP_Request_URL_Prefix];
    NSDictionary *parameters = @{@"language": @"CHINESE"};
    
    [loginObj requestGetPath:urlPath Parameter:parameters withVC:self timeout:5];
    loginObj.myDataBlock = ^(NSDictionary *dataDic){
        
        NSString *loginCode = dataDic[@"status"];
        if (loginCode.integerValue == 1) {
            NSDictionary *objDic = dataDic[@"data"];
            _searchObj = [HDSearchObj searchObjWithDict:objDic];
            [_myTableView reloadData];
        }
        else {
        }
    };
}

#pragma mark
#pragma mark -------- Tableview Methods --------
#define Top_View_H 160.0f

- (void)tableviewInit {
    _myTableView.delegate = self;
    _myTableView.dataSource = self;
    _myTableView.rowHeight = 100;
    _myTableView.separatorColor = [UIColor clearColor];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _searchObj.comment_infoArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    HDRootSearchCell *cell = [[[NSBundle mainBundle] loadNibNamed:@"HDRootSearchCell" owner:self options:nil] lastObject];
    
    HDSearchListObj *obj = _searchObj.comment_infoArray[indexPath.row];
    cell.contentLabel.text = obj.title;
    cell.contentLabel.font = [UIFont fontWithName:@"FangSong" size:15.0f];

    [cell.imgView sd_setImageWithURL:[NSURL URLWithString:obj.comment_img] placeholderImage:[UIImage imageNamed:@"mapview_bg"]];
    [cell.photoImgView sd_setImageWithURL:[NSURL URLWithString:obj.user_head] placeholderImage:[UIImage imageNamed:@"mapview_bg"]];
    cell.timeLabel.text = obj.time;
    [cell.lookBtn setTitle:[NSString stringWithFormat:@"%@",obj.look_num] forState:UIControlStateNormal];
    [cell.sayBtn setTitle:[NSString stringWithFormat:@"%@",obj.comment_num]forState:UIControlStateNormal];
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section { return 180.f; }

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    UIView *bgView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.frame.size.width, 180.f)];
    bgView.backgroundColor = _topBar.backgroundColor;

    HDBtnView *topView = [[[NSBundle mainBundle] loadNibNamed:@"HDBtnView" owner:self options:nil] lastObject];
    topView.backgroundColor = _topBar.backgroundColor;
    topView.delegate = self;
    [bgView addSubview:topView];
    [topView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(bgView).with.offset(0);
        make.right.equalTo(bgView).with.offset(0);
        make.left.equalTo(bgView).with.offset(0);
        make.bottom.equalTo(bgView).with.offset(-20);
    }];
    
    return bgView;
}

- (void)topBtnClick:(UIButton *)btn {
    NSInteger btnTag = btn.tag;
    NSString *webStr = nil;
    NSString *webName = nil;
    switch (btnTag) {
        case 501:
            webStr = _searchObj.web_site;
            webName = @"官网";
            break;
        case 502:
            webStr = _searchObj.weixin_url;
            webName = @"市集";
            break;
        case 503:
            webStr = _searchObj.taobao_url;
            webName = @"淘宝店铺";
            break;
        case 504:
            webStr = _searchObj.ticket_url;
            webName = @"门票预订";
            break;
        case 505:
            webStr = _searchObj.parking_url;
            webName = @"停车位置";
            break;
        case 506: // 酒店
        {
            UIStoryboard *board = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
            HDAroundVC *pushVC = [board instantiateViewControllerWithIdentifier:@"HDAroundVC"];
            pushVC.listType = kList_Type_Hotel;
            webName = @"周边酒店";
            [self.navigationController pushViewController:pushVC animated:YES];
        }
            break;
        case 507: // 餐饮
        {
            UIStoryboard *board = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
            HDAroundVC *pushVC = [board instantiateViewControllerWithIdentifier:@"HDAroundVC"];
            pushVC.listType = kList_Type_Eating;
            webName = @"周边餐饮";
            [self.navigationController pushViewController:pushVC animated:YES];
        }
            break;
        case 508:
            webStr = _searchObj.leave_msg;
            webName = @"意见反馈";
            break;
 
        default:
            break;
    }
    
    if(webStr != nil) {
        UIStoryboard *board = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
        HDWebVC *webVC = [board instantiateViewControllerWithIdentifier:@"HDWebVC"];
        webVC.webPath = webStr;
        UIView *view = webVC.view;
        if (view) {
            webVC.nameLabel.text = webName;
        }
        
        [self.navigationController pushViewController:webVC animated:YES];
    }
}

- (void)pushToFeedbackAction
{
    
}

#pragma mark
#pragma mark -------- Dealloc Methods --------


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end
